package com.mvp.shq.washer.ui.shop.order.orderpay;


import com.mvp.shq.washer.entity.result.AlipayResult;
import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.result.WxPayResult;
import com.mvp.shq.washer.entity.shop.ProductUnit;
import com.mvp.shq.washer.entity.shop.TotalOrderData;
import com.mvp.shq.washer.entity.shop.TotalOrderList;
import com.mvp.shq.washer.entity.shop.TotalOrderListData;
import com.mvp.shq.washer.entity.shop.result.OrderDataListResult;
import com.mvp.shq.washer.entity.shop.result.OrderDataResult;
import com.mvp.shq.washer.entity.shop.result.OrderDetailListRestult;
import com.mvp.shq.washer.entity.shop.result.ShopAliPayResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by westing on 2017/10/11.
 */
public class Presenter extends OrderPayConstract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    void alipay(String orderNo) {
        String token = SpUtil.getToken();
        model.getAlipayResult(token,orderNo, DataDictionary.SHOP_PAY_ALIPAY)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<ShopAliPayResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<ShopAliPayResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.alipayS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void wxPay(String orderNo) {
        String token = SpUtil.getToken();
        model.getWxResult(token,orderNo, DataDictionary.SHOP_PAY_WX)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<WxPayResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<WxPayResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.wxPayS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void createOrder(OrderDataListResult result) {
        List<TotalOrderListData> ecOrderDtoList = new ArrayList<>();

        TotalOrderListData totalOrderList = new TotalOrderListData();
        totalOrderList.setCompanyId(result.getCompanyId());
        totalOrderList.setPayMode(result.getPayMode());
        List<ProductUnit> ecProductUnitDtoList = new ArrayList<>();
        List<OrderDetailListRestult> orderDetailListRestult = result.getEcOrderDetailList();
        Logger.d("商品数据:"+orderDetailListRestult.size());
        for (int i=0;i<orderDetailListRestult.size();i++){
            ProductUnit unit = new ProductUnit(orderDetailListRestult.get(i).getNum(),orderDetailListRestult.get(i).getProductUnitId());
            ecProductUnitDtoList.add(unit);
        }
        totalOrderList.setEcProductUnitDtoList(ecProductUnitDtoList);
        ecOrderDtoList.add(totalOrderList);
        String isMultip = "0";
        TotalOrderData data = new TotalOrderData(result.getAddressDes(),result.getAreaId(),result.getCashPrice(),result.getCityId(),
                ecOrderDtoList,result.getIntegralPrice(),isMultip,result.getId(),result.getProvinceId(),
                result.getCashPrice(),result.getUnitId(),result.getUserId(),result.getConsigneeMobile(),result.getConsigneeName());
        data.setId(result.getId());
        String token = SpUtil.getToken();
        model.createOrder(token,data)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<OrderDataResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<OrderDataResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.createOrder(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                    }
                });
    }

    @Override
    void getOrderInfo(String orderId) {
        String token = SpUtil.getToken();
        String userId = SpUtil.getUser().getId();
        model.getOrderInfo(token,userId,orderId)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<OrderDataListResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<OrderDataListResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getOrderInfoS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                    }
                });
    }
}
